package com.ctgu.controller;


import com.ctgu.entity.Client;
import com.ctgu.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("client")
public class ClientController {
    @Autowired
    @Qualifier("ClientServiceImpl")
    private ClientService  clientService;

    @RequestMapping("allClient")
    public String list(Model model){
        List<Client> list = clientService.getAllClientList();
        model.addAttribute("list",list);
        return "allClient";
    }

    @RequestMapping("toAddClient")
    public String toAddPage(){
        return "addClient";
    }

    @RequestMapping("addClient")
    public String addClient(Client client){
        clientService.addClient(client);
        return "redirect:allClient";
    }

    @RequestMapping("toUpdatePage")
    public String  toUpdatePage(int id,Model model){
        Client client = clientService.getClient(id);
        model.addAttribute("getClient",client);
        return "updateClient";
    }

    @RequestMapping("updateClient")
    public String updateClient(Client client){
        clientService.updateClient(client);
        return "redirect:allClient";
    }

    @RequestMapping("deleteClient")
    public String deleteClient(int id){
        clientService.deleteClientById(id);
        return "redirect:allClient";
    }

    @RequestMapping("queryClient")
    public String queryClient( String name,Model model){
        List<Client> list = clientService.queryClientById(name);
        if(list.size()==0){
            list = clientService.getAllClientList();
            model.addAttribute("error","未查到");
        }
        model.addAttribute("list",list);
        return "allClient";
    }

}
